//
//  UIView+GBExtent.m
//  DrawingTest
//
//  Created by chenguibang on 16/4/1.
//  Copyright © 2016年 chenguibang. All rights reserved.
//

#import "UIView+GBExtent.h"

@implementation UIView(GBExtent)

-(void)addCornerWithType:(CornerType)type color:(UIColor*)color radius:(CGFloat)radius{
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    //    [shapeLayer setBounds:self.bounds];
    //    [shapeLayer setPosition:self.center];
    [shapeLayer setFillColor:[color CGColor]];
    // 设置虚线颜色为blackColor
    [shapeLayer setStrokeColor:[color CGColor]];
    //    [shapeLayer setStrokeColor:[[UIColor colorWithRed:223/255.0 green:223/255.0 blue:223/255.0 alpha:1.0f] CGColor]];
    // 3.0f设置虚线的宽度
    [shapeLayer setLineWidth:0.0f];
    [shapeLayer setLineJoin:kCALineJoinRound];
    
    
    //=== 绘画逻辑 ===
    
    //创建用于转移坐标的Transform，这样我们不用按照实际显示做坐标计算
    
    //    CGAffineTransform transform = CGAffineTransformMakeTranslation(0, -200);
    
    //创建CGMutablePathRef
    
    CGMutablePathRef path = CGPathCreateMutable();
    
//    //半径为30
//    //初始点为(0, 0)
//    CGPathMoveToPoint(path, NULL,self.frame.size.width, 0);
//    CGPathAddLineToPoint(path, NULL,self.frame.size.width/2, 0);
//    //右上角和右下角两个点，画出半个圆角
//    CGPathAddArcToPoint(path, NULL, self.frame.size.width, 0, self.frame.size.width, self.frame.size.height/2, radius);
//    //右下角和左下角两个点，画出另外半个圆角
//    //    CGPathAddArcToPoint(path, NULL, self.frame.size.width, self.frame.size.height, self.frame.size.width/2, self.frame.size.height, radius);
//    //    //CGPathAddArcToPoint会在交点中停止，所以需要再次调用CGPathAddLineToPoint画出下面的线
//    //    CGPathAddLineToPoint(path, NULL, self.frame.size.width, self.frame.size.height);
// 
    
   
    

    
    
    
    
    
    
    if (type&1 << 1) {
        NSLog(@"CornerTypeTopLeft");
        CGPathMoveToPoint(path, NULL,0, 0);
        CGPathAddLineToPoint(path, NULL,self.frame.size.width/2, 0);
        CGPathAddArcToPoint(path, NULL, 0, 0, 0, self.frame.size.height/2, radius);
        
    }
    
    if (type&1 << 2){
         NSLog(@"CornerTypeTopRight");
        
        CGPathMoveToPoint(path, NULL,self.frame.size.width, 0);
        CGPathAddLineToPoint(path, NULL,self.frame.size.width/2, 0);
        //右上角和右下角两个点，画出半个圆角
        CGPathAddArcToPoint(path, NULL, self.frame.size.width, 0, self.frame.size.width, self.frame.size.height/2, radius);
    }
    
    if (type&1 << 3){
         NSLog(@"CornerTypeBottomLeft");
        NSLog(@"CornerTypeTopLeft");
        CGPathMoveToPoint(path, NULL,0, self.frame.size.height);
        CGPathAddLineToPoint(path, NULL,self.frame.size.width/2, self.frame.size.height);
        CGPathAddArcToPoint(path, NULL, 0, self.frame.size.height, 0, self.frame.size.height/2, radius);
    }
    
    if (type&1 << 4){
       
        CGPathMoveToPoint(path, NULL,self.frame.size.width, self.frame.size.height);
        CGPathAddLineToPoint(path, NULL,self.frame.size.width/2, self.frame.size.height);
        
        CGPathAddArcToPoint(path, NULL, self.frame.size.width, self.frame.size.height, self.frame.size.width, self.frame.size.height/2, radius);
    }
    [shapeLayer setPath:path];
    CGPathRelease(path);
//    shapeLayer.backgroundColor = [UIColor greenColor].CGColor;
    [self.layer addSublayer:shapeLayer];
    
}


-(void)addBorderWithType:(BorderType)type color:(UIColor *)color width:(CGFloat)width{
    
    
    [self layoutIfNeeded];
    [self setNeedsLayout];
    CGFloat ss =  self.frame.size.width;
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    [shapeLayer setFillColor:[color CGColor]];
    [shapeLayer setStrokeColor:[color CGColor]];
    [shapeLayer setLineWidth:width];
    [shapeLayer setLineJoin:kCALineJoinRound];
    CGMutablePathRef path = CGPathCreateMutable();
    
    if (type&BorderTop) {
        CGPathMoveToPoint(path, NULL,0, 0);
        CGPathAddLineToPoint(path, NULL,self.frame.size.width, 0);
    }
    
    if (type&BorderRight){
    
        
        CGPathMoveToPoint(path, NULL,self.frame.size.width, 0);
        CGPathAddLineToPoint(path, NULL,self.frame.size.width, self.frame.size.height);
       
    }
    
    if (type&BorderLeft){
        CGPathMoveToPoint(path, NULL,0, 0);
        CGPathAddLineToPoint(path, NULL,0, self.frame.size.height);
        
    }
    
    if (type&BorderButtom){
        
        CGPathMoveToPoint(path, NULL,0, self.frame.size.height);
        CGPathAddLineToPoint(path, NULL,self.frame.size.width, self.frame.size.height);
    }
    
    [shapeLayer setPath:path];
    CGPathRelease(path);
    shapeLayer.backgroundColor = [UIColor greenColor].CGColor;
    [self.layer addSublayer:shapeLayer];
    

}


-(void)addDashWithType:(DashType)type color:(UIColor *)color width:(CGFloat)width itemlenght:(int)lenght spacing:(int)space{
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    [shapeLayer setFillColor:[color CGColor]];
    [shapeLayer setStrokeColor:[color CGColor]];
    [shapeLayer setLineWidth:width];
    [shapeLayer setLineJoin:kCALineJoinRound];
    
    [shapeLayer setLineDashPattern:
    [NSArray arrayWithObjects:[NSNumber numberWithInt:lenght],
    [NSNumber numberWithInt:space],nil]];
    
    CGMutablePathRef path = CGPathCreateMutable();
    
    if (type&DashTop) {
        CGPathMoveToPoint(path, NULL,0, 0);
        CGPathAddLineToPoint(path, NULL,self.frame.size.width, 0);
    }
    
    if (type&DashRight){
        
        
        CGPathMoveToPoint(path, NULL,self.frame.size.width, 0);
        CGPathAddLineToPoint(path, NULL,self.frame.size.width, self.frame.size.height);
        
    }
    
    if (type&DashLeft){
        CGPathMoveToPoint(path, NULL,0, 0);
        CGPathAddLineToPoint(path, NULL,0, self.frame.size.height);
        
    }
    
    if (type&DashButtom){
        
        CGPathMoveToPoint(path, NULL,0, self.frame.size.height);
        CGPathAddLineToPoint(path, NULL,self.frame.size.width, self.frame.size.height);
    }
    [shapeLayer setPath:path];
    CGPathRelease(path);
    shapeLayer.backgroundColor = [UIColor greenColor].CGColor;
    [self.layer addSublayer:shapeLayer];

}


@end
